<?php 
// +----------------------------------------------------------------------
// | @projectName  【order_system---点餐系统】
// +----------------------------------------------------------------------
// | @author        山西创客空间科技有限公司
// +----------------------------------------------------------------------
// | @date          2017年10月13日 星期五
// +----------------------------------------------------------------------
// | @Copyright     http://sx-ck.com All rights reserved.      
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\admin\model\Common;
use app\common\Base;
use app\common\Error;
use think\Controller;
use think\Db;
use think\Request;

class DinnerFloor extends Base
{
	private $sql = "SELECT a.id AS dinner_id,a.name AS dinner_name,b.id AS floor_id,b.name AS floor_name,CONCAT(a.name,b.name) AS nameinfo FROM dinner a LEFT JOIN window c ON c.dinner_id=a.id LEFT JOIN floor b ON b.id=c.floor_id";

	private $sql2 = " SELECT a.*,CASE a.is_half WHEN 0 THEN '否' ELSE '是' END AS is_half_info, 
                    CASE a.meals_time_id WHEN 0 THEN '早餐' WHEN 1 THEN '午餐' WHEN 2 THEN '晚餐' ELSE '' END AS meals_time_info,
                    CONCAT(d.`name`,'-',e.`name`,'-',b.`code`,'号窗口') AS name_info,CONCAT(a.`start_time`,'到',a.`end_time`) AS start_end,
                    b.`code` AS window_code,b.ip,c.advice_price,c.`name` AS cook_menu_name,CONCAT(\"/static/upload/cook_menu/\",c.img_url) AS img_url,f.`code` AS meals_type_code,
                    CONCAT(\"/static/upload/meals_type/\",f.img_url) AS meals_type_img,f.`name` AS meals_type_name   FROM garnish a LEFT JOIN window b ON a.window_id = b.id 
                    LEFT JOIN cook_menu c ON a.cook_menu_id = c.id LEFT JOIN dinner d ON d.id = b.dinner_id LEFT JOIN floor e ON e.id = b.floor_id LEFT JOIN meals_type f ON c.meals_type_id = f.id ";

	//查询餐厅和餐厅对应的楼层
	public function page_show()
	{
		$dbname = Db::name('dinner');
		$result = $dbname -> query($this -> sql);
		if ($result === false) {
			return Base::echo_error(Error::DB_ERROR);
		}
		return Base::echo_success($result);
	}

	//查询某餐厅某层的窗口+菜
	public function show_garnish()
	{
		$window = Db::name('window');
		//检测餐厅是否合法
		$dinner_id = $this -> request -> post('dinner_id');
		// $dinner_id = '0C6EC14D-6CCA-4B59-8BE4-782B5E1EA1BE';
		if (Common::check_empty($dinner_id) ===  true) {
			return Base::echo_error(Error::DINNER_ID_IS_EMPTY);
		}

		//检测楼层是否合法
		$floor_id = $this -> request -> post('floor_id');
		// $floor_id = 'D08761F0-AC30-424C-90F3-FB56044BC11F';
		if (Common::check_empty($floor_id) === true) {
			return Base::echo_error(Error::FLOOR_ID_IS_EMPTY);
		}

		//查询窗口
		$win_sql = "SELECT a.id,a.code FROM window a WHERE a.dinner_id='$dinner_id' AND a.floor_id='$floor_id'";
		$result = $window -> query($win_sql);

		//检测当前日期
        $date = $this -> request -> post('date');
        // $date = "2017-11-01";
        if (Common::check_empty($date) === true) {
            return Base::echo_error(Error::DATE_IS_EMPTY);
        }
        $row = [];
		$garnish = Db::name('garnish');

		foreach ($result as $key => $resultVal) {
			$del_sql = " SELECT a.* FROM garnish a WHERE a.adjust_time IS NOT NULL AND a.adjust_time != '$date' ";
        	$del_row = $garnish -> query($del_sql);
	        foreach ($del_row as $key => $value) {
	            $garnish -> where('id','=',"{$value['id']}") -> delete();
	        }
	        $garnish_row = $garnish -> query($this ->sql2."WHERE b.id = \"{$resultVal['id']}\" AND '$date' >= a.start_time AND '$date' <= a.end_time");
			if ($garnish_row === false) {
				return Base::echo_error(Error::DB_ERROR);
			}
			
			$row[$key] = ['window_id' => $resultVal['id'],'window_name' => $resultVal['code'].'号窗口','garnish_arr' => $garnish_row ];

		}
		return Base::echo_success($row);
	  
	}

	// 获取餐厅的接口
    public function get_dinner() 
    {
        $dinner = Db::name('dinner');
        $result = $dinner -> select();
        if ($result === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        return $this -> echo_success ($result);
    }

    // 获取楼层的接口
    public function get_floor() 
    {
        $floor = Db::name('floor');
        $result = $floor -> select();
        if ($result === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        return $this -> echo_success ($result);
    }
	
}
 ?>
